
local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseObject());
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local EggLuckGui = _G.ClientOHOH.load.UIManager():CreateUI("EggLuckGui",require(script.EggLuckGui))
local LuckShowCtrl=nil
local CellClass=require(script.Parent.Cell)

function M.new(self,...)
	return M(self,...);
end;


function M.ctor(self,eggCfg,att,eggluckCtrl,player)
    self.cfg=eggCfg
    self.ctrl=eggluckCtrl
    self.att=att
    self.player=player
    self.DataBase=self.player:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent());
    self.EggId=self.cfg.id

    self.priceIcon=_G.CommonOHOH.load.ItemTool().GetItemShowView(self.cfg.costId)
    self.price=_G.ClientOHOH.load.BigNumberHelper():ToString(self.cfg.price)

    local probability=GeneralTool.GetProbabilityShowPercent(self.cfg.probability)
    self.CellList={}
    for k,v in pairs(self.cfg.petIds) do
       local petId= _G.CommonOHOH.load.PetTool().GetPetId(v)
       local guid=self:GetGuid(k,petId)
       local cell=CellClass.new(petId,guid,nil,self.DataBase)
       cell:SetProbability(probability[k])
       cell.ClickEvent:Connect(function()
            cell:ChangeSell()
       end)
       self.CellList[guid]=cell
    end

    if not LuckShowCtrl then
        LuckShowCtrl=EggLuckGui:AddPrivateComponent(script.LuckCtrl)
        LuckShowCtrl:Init(self.ctrl,player)
    end
end;


-- {Lucky1={lucky=100,IsHad=true},Lucky2={lucky=100,IsHad=true},Lucky3={lucky=100,IsHad=true}
-- }


function M.GetGuid(self,index,petId)
    return  self.EggId.."_"..index
end

function M.Show(self)
    if  self.isShowing then
        return 
    end
    
    self.ctrl:Close_T()

    self.isShowing=true

    local parent=EggLuckGui:GetCellParent()

    for _,cell in pairs(self.CellList) do
        cell:SetSellStatus(false)
        cell:SetParent(parent)
    end

    EggLuckGui:Show(self.att,self.priceIcon, self.price)

    if self.Conn then
        self.Conn:Disconnect()
        self.Conn=nil
    end

    self.Conn=EggLuckGui.HatchEvent:Connect(function(id)
        self:Hatch(id)
    end)

end

function M.Hatch(self,id)
    assert(self.ctrl[id],"No Hatch ID:"..id)
    self.ctrl:Close_T()
    self.ctrl[id](self.ctrl,self.EggId,self:GetDeletePetID())
end


function M.GetDeletePetID(self)

    local data={}

    for _,cell in pairs(self.CellList) do
        if cell:isSelling() then
            data[cell:GetPetId()]=true
        end
       
    end
    return data
end

function M.Hide(self)
    if not self.isShowing then
        return 
    end

    self.ctrl:Close_T()

    self.isShowing=nil

    for _,cell in pairs(self.CellList) do
        cell:SetParent(nil)
        cell:ClearMouseEnter(nil)
    end

    EggLuckGui:Hide()

    if self.Conn then
        self.Conn:Disconnect()
        self.Conn=nil
    end

end

function M.dtor(self)
	M.super.dtor(self);
end;




return M;
